package com.ktgj.sms.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.sms.domain.SmsTemplate;
import com.ktgj.sms.service.ISmsTemplateService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 短信模板Controller
 * 
 * @author ruoyi
 * @date 2021-08-11
 */
@ControllerAdvice
@RestController
@RequestMapping("/template")
public class SmsTemplateController extends BaseController
{
    @Autowired
    private ISmsTemplateService smsTemplateService;

    /**
     * 查询短信模板列表
     */
    @PreAuthorize(hasPermi = "system:template:list")
    @GetMapping("/list")
    public TableDataInfo list(SmsTemplate smsTemplate)
    {
        startPage();
        List<SmsTemplate> list = smsTemplateService.selectSmsTemplateList(smsTemplate);
        return getDataTable(list);
    }

    /**
     * 导出短信模板列表
     */
    @PreAuthorize(hasPermi = "system:template:export")
    @Log(title = "短信模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsTemplate smsTemplate) throws IOException
    {
        List<SmsTemplate> list = smsTemplateService.selectSmsTemplateList(smsTemplate);
        ExcelUtil<SmsTemplate> util = new ExcelUtil<SmsTemplate>(SmsTemplate.class);
        util.exportExcel(response, list, "短信模板数据");
    }

    /**
     * 获取短信模板详细信息
     */
//    @PreAuthorize(hasPermi = "system:template:query")
    @GetMapping(value = "/{smsTemplateId}")
    public AjaxResult getInfo(@PathVariable("smsTemplateId") Long smsTemplateId)
    {
        return AjaxResult.success(smsTemplateService.selectSmsTemplateById(smsTemplateId));
    }

    /**
     * 查询呆修改的信息
     * */
    @PostMapping("/getTemplate")
    public AjaxResult getTemplate(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        SmsTemplate smsTemplate = smsTemplateService.selectSmsTemplateById(id);
        return AjaxResult.success(smsTemplate);
    }

    /**
     * 查询待修改的信息（远程调用）
     * @param smsTemplateId
     * @return
     */
    @GetMapping("/getTemplateBySmsTemplateId")
    public AjaxResult getTemplateBySmsTemplateId(@RequestParam("smsTemplateId") Long smsTemplateId) {
        return AjaxResult.success(smsTemplateService.querySmsTemplateById(smsTemplateId));
    }

    /**
     * 新增短信模板
     */
    @PostMapping("/add")
    public AjaxResult add(HttpServletRequest request, @RequestBody SmsTemplate smsTemplate) {
        int i;
        try {
            i = smsTemplateService.insertSmsTemplate(smsTemplate, request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 修改短信模板
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SmsTemplate smsTemplate) {
        int i = smsTemplateService.updateSmsTemplate(smsTemplate);
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除短信模板
     */
	@PostMapping("/remove")
    public AjaxResult remove(@RequestBody Map<String, Object> map) {
	    Long id = Long.valueOf(map.get("id").toString());
        SmsTemplate smsTemplate = new SmsTemplate();
        smsTemplate.setSmsTemplateId(id);
        smsTemplate.setIsDelete("true");
        int i = smsTemplateService.updateSmsTemplate(smsTemplate);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 短信模板列表
     * */
    @PostMapping("/smsTemplateList")
    public TableDataInfo smsTemplateList(HttpServletRequest request, @RequestBody SmsTemplate smsTemplate){
        startPage();
        List<SmsTemplate> smsTemplateList;
        try {
            smsTemplateList = smsTemplateService.smsTemplateList(smsTemplate, request);
        } catch (Exception e){
            return null;
        }

        return getDataTable(smsTemplateList);
    }
}
